using MicroMsg.Common.Utils;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
namespace MicroMsg.UI.Page
{
	public class WebMMLoginPage : BasePage
	{
		private const string TAG = "WebMMLoginPage";
		private bool isClickedOK;
		private bool hasBeenInvokLoadedComplete;
		private Stack<Uri> NavigationStack = new Stack<Uri>();
		internal Grid LayoutRoot;
		internal WebBrowser webView;
		private bool _contentLoaded;
		public WebMMLoginPage()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Page_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Page_Unloaded));
		}
		private void Page_Loaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("WebMMLoginPage", "Page_Loaded");
		}
		private void Page_Unloaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("WebMMLoginPage", "Page_Unloaded");
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("WebMMLoginPage", "OnPageIn");
			if (object_0 == null || object_0.Length == 0)
			{
				return;
			}
			if (object_0[0] is string)
			{
				string text = object_0[0] as string;
				try
				{
					Uri uri = new Uri(text);
					this.webView.Navigate(uri);
				}
				catch
				{
					base.GoBack();
				}
				return;
			}
			base.GoBack();
		}
		private void webView_Navigated(object sender, NavigationEventArgs e)
		{
			this.doloadedComplete();
		}
		private void webView_Navigating(object sender, NavigatingEventArgs e)
		{
			if (e != null && !(e.get_Uri() == null))
			{
				if (e.get_Uri().get_LocalPath() == "/success")
				{
					this.isClickedOK = false;
					this.doLoginSeccess();
				}
				return;
			}
		}
		private void webView_loaded(object sender, EventArgs e)
		{
			if (this.webView.get_Height() < 100.0)
			{
				this.webView.set_Height(700.0);
				this.webView.set_Margin(new Thickness(0.0, 100.0, 0.0, 0.0));
			}
		}
		private void webView_NavigationFailed(object sender, NavigationFailedEventArgs e)
		{
			if (e == null)
			{
				return;
			}
			if (this.isClickedOK)
			{
				this.isClickedOK = false;
				this.doLoginSeccess();
				return;
			}
			base.GoBack();
		}
		private void doLoginSeccess()
		{
			PageManager.getInstance().ClearStackToChatPage();
			if (MainPage.mainPageInstance != null)
			{
				MainPage.mainPageInstance.ChangeToSessionView();
			}
		}
		private void webView_ScriptNotify(object sender, NotifyEventArgs e)
		{
			string text = e.get_Value().ToString();
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			if (text == "onOK")
			{
				this.isClickedOK = true;
				return;
			}
			if (text == "onCancel")
			{
				base.GoBack();
			}
		}
		private void doloadedComplete()
		{
			if (this.hasBeenInvokLoadedComplete)
			{
				return;
			}
			try
			{
				this.webView.InvokeScript("eval", new string[]
				{
					"\r\n                window.onOK = function() {\r\n                    var elem = event.srcElement;\r\n                    if ( elem != null ) {\r\n                        window.external.notify('onOK');\r\n                    }\r\n                    return false;\r\n                }\r\n\r\n                window.onCancel = function() {\r\n                    var elem = event.srcElement;\r\n                    if ( elem != null ) {\r\n                        window.external.notify('onCancel');\r\n                    }\r\n                    return false;\r\n                }\r\n\r\n                window.BindLinks = function() {\r\n                    var inputs = document.all.tags('input');\r\n                    for (var i = 0; i < inputs.length; i++)\r\n                    {\r\n                        var input = inputs[i];\r\n                        if(input.name == 'confirm')\r\n                        {\r\n                            input.attachEvent('onmousedown', onOK);\r\n                        }\r\n                    }\r\n\r\n                    var elems = document.getElementsByTagName('a');\r\n                    for (var i = 0; i < elems.length; i++) \r\n                    {\r\n                        var elem = elems[i];\r\n\t\t\t\t\t    if( elem.href.indexOf('scanqrcode') != -1)\r\n\t\t\t\t\t    {\r\n\t\t\t\t\t         elem.attachEvent('onmousedown', onCancel);\r\n\t\t\t\t\t    }\r\n                    }\r\n                }\r\n                "
				});
				this.webView.InvokeScript("BindLinks");
				this.hasBeenInvokLoadedComplete = true;
			}
			catch (Exception)
			{
				Log.smethod_1("WebMMLoginPage", "webView.InvokeScript failed!");
			}
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
		}
		public static void GotoThisPage(string string_0)
		{
			ToolFunction.GotoPageForUserControl("/Source/UI/WebMM/WebMMLoginPage.xaml", new object[]
			{
				string_0
			}, false);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/WebMM/WebMMLoginPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.webView = (WebBrowser)base.FindName("webView");
		}
	}
}
